//
//  SWBaseTabBarController.m
//  SinaWeibo2
//
//  Created by zhuangqianwei on 16/11/28.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

#import "SWBaseTabBarController.h"
#import "SWNavigationController.h"
#import "SWHomeViewController.h"
#import "SWTabBarLayout.h"

@interface SWBaseTabBarController ()<SWTabBarDelegate>

@end

@implementation SWBaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildViewControllers];
    //想要修改系统的某些属性，但被设置了readOnly，就用KVC，即：setValue： forKey：
    //修改tabBar为自动义的tabbar
    SWTabBarLayout * tabBar = [[SWTabBarLayout alloc]init];
    tabBar.delegate = self;
    [self setValue:tabBar forKey:@"tabBar"];


}

- (void)addChildViewControllers {
    NSMutableArray * arrayM = [NSMutableArray array];
    [arrayM addObject:[self controllerWithClassName:@"SWHomeViewController" title:@"首页" imageName:@"tabbar_home" index:0]];
    [arrayM addObject:[self controllerWithClassName:@"SWMessageViewController" title:@"消息" imageName:@"tabbar_message_center" index:1]];
    [arrayM addObject:[self controllerWithClassName:@"SWDiscoverViewController" title:@"发现" imageName:@"tabbar_discover" index:2]];
    [arrayM addObject:[self controllerWithClassName:@"SWProfileViewController" title:@"我" imageName:@"tabbar_profile" index:3]];
    
    
    self.viewControllers = arrayM.copy;
   }


//封装方法
- (UIViewController *)controllerWithClassName: (NSString *)className title: (NSString *)title imageName: (NSString *)imageName index: (int)index {
    
    Class cls = NSClassFromString(className);
    NSAssert([cls isSubclassOfClass:[UIViewController class]], @"类名错误");
    UIViewController * vc = [cls new];
//    vc.title = title;
    vc.tabBarItem.tag = index;
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]}forState:UIControlStateSelected];
    [vc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}forState:UIControlStateNormal];
    vc.tabBarItem.title = title;
    vc.navigationItem.title = title;
    vc.tabBarItem.badgeValue = @"8";
    vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3);
    vc.tabBarItem.badgeColor = [UIColor lightGrayColor];
    vc.tabBarItem.image = [[UIImage imageNamed:imageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:@"_selected"]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    SWNavigationController * nav = [[SWNavigationController alloc]initWithRootViewController:vc];
    return  nav;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
    //应该获取到被点击的UITabbarButton
    int index = 0;
    for (UITabBar * subview in tabBar.subviews) {
        if ( [subview isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (index == item.tag) {
                //可以获取到被点击的tabbarButton
                for (UIView * v in subview.subviews) {
                    if ([v isKindOfClass:NSClassFromString(@"UIImageView")]) {
                        NSLog(@"终于找到你");
                        //先缩小
                        v.transform = CGAffineTransformMakeScale(0.6, 0.6);
                        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:6 options:0 animations:^{
                            
                            v.transform = CGAffineTransformIdentity;
                        } completion:nil];
                    }
                }
                
                
            }
            index += 1;
        }
    }
    
    
    
    
}




//实现代理方法
- (void)btnDidClickPush:(SWTabBarLayout *)tabbar {
    
    //点击事件内容
    UIViewController *vc = [[UIViewController alloc]init];
    [self presentViewController:vc animated:YES completion:nil];
    
    
}

@end
